package com.example.teaching_server.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/**
 * 时间线项目数据传输对象
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "时间线项目信息")
public class TimelineItemDTO {

    @NotBlank(message = "日期不能为空")
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Schema(description = "活动日期", example = "2024-03-01", requiredMode = Schema.RequiredMode.REQUIRED)
    private String date;

    @NotBlank(message = "时间不能为空")
    @JsonFormat(pattern = "HH:mm")
    @Schema(description = "活动时间", example = "14:30", requiredMode = Schema.RequiredMode.REQUIRED)
    private String time;

    @NotBlank(message = "活动名称不能为空")
    @Size(max = 100, message = "活动名称长度不能超过100个字符")
    @Schema(description = "活动名称", example = "数学课堂练习", requiredMode = Schema.RequiredMode.REQUIRED)
    private String activityName;

    @Size(max = 500,message = "活动描述长度不能超过500个字符")
    @Schema(description = "活动描述", example = "完成第一章数学练习题")
    private String description;

    @Schema(description = "图片URL列表")
    private List<String> imageUrls;

    @Schema(description = "活动类型", example = "class", allowableValues = {"class", "homework", "exam", "activity"})
    private String activityType;

    @Schema(description = "活动状态", example = "completed", allowableValues = {"pending", "in_progress", "completed", "cancelled"})
    private String status;

    @Schema(description = "活动地点", example = "教学楼201")
    private String location;

    @Schema(description = "活动时长（分钟）", example = "45")
    private Integer duration;

    @Schema(description = "参与人数", example = "35")
    private Integer participants;

    @Schema(description = "是否重要", example = "false")
    @Builder.Default
    private Boolean important = false;

    @Schema(description = "是否已完成", example = "true")
    @Builder.Default
    private Boolean completed = true;

    /**
     * 获取完整的日期时间
     */
    @Schema(description = "完整日期时间", accessMode = Schema.AccessMode.READ_ONLY)
    public String getFullDateTime() {
        return date + " " + time;
    }

    /**
     * 获取活动类型中文名称
     */
    @Schema(description = "活动类型中文名称", accessMode = Schema.AccessMode.READ_ONLY)
    public String getActivityTypeName() {
        return switch (activityType) {
            case "class" -> "课堂";
            case "homework" -> "作业";
            case "exam" -> "考试";
            case "activity" -> "活动";
            default -> activityType;
        };
    }

    /**
     * 获取状态中文名称
     */
    @Schema(description = "状态中文名称", accessMode = Schema.AccessMode.READ_ONLY)
    public String getStatusName() {
        return switch (status) {
            case "pending" -> "待开始";
            case "in_progress" -> "进行中";
            case "completed" -> "已完成";
            case "cancelled" -> "已取消";
            default -> status;
        };
    }

    /**
     * 判断是否有图片
     */
    @Schema(description = "是否有图片", accessMode = Schema.AccessMode.READ_ONLY)
    public boolean hasImages() {
        return imageUrls != null && !imageUrls.isEmpty();
    }

    /**
     * 获取图片数量
     */
    @Schema(description = "图片数量", accessMode = Schema.AccessMode.READ_ONLY)
    public int getImageCount() {
        return hasImages() ? imageUrls.size() : 0;
    }

    /**
     * 获取活动时长描述
     */
    @Schema(description = "活动时长描述", accessMode = Schema.AccessMode.READ_ONLY)
    public String getDurationDescription() {
        if (duration == null) return "未知";
        if (duration < 60) {
            return duration + "分钟";
        } else {
            return (duration / 60) + "小时" + (duration % 60) + "分钟";
        }
    }

    /**
     * 构建基础时间线项目
     */
    public static TimelineItemDTO basic(String date, String time, String activityName, String description) {
        return TimelineItemDTO.builder()
                .date(date)
                .time(time)
                .activityName(activityName)
                .description(description)
                .important(false)
                .completed(true)
                .build();
    }

    /**
     * 构建课堂时间线项目
     */
    public static TimelineItemDTO classActivity(String date, String time, String activityName, String location) {
        return TimelineItemDTO.builder()
                .date(date)
                .time(time)
                .activityName(activityName)
                .activityType("class")
                .location(location)
                .duration(45)
                .important(true)
                .completed(true)
                .build();
    }
}
